package com.hui.controller.user;

import cn.hutool.core.util.NumberUtil;
import com.hui.entity.enums.AppHttpCodeEnum;
import com.hui.entity.vo.StoreProductRelationQueryVo;
import com.hui.handler.exception.SystemException;
import com.hui.model.storeproduct.StoreProductRelationQueryParam;
import com.hui.model.user.CollectQueryParam;
import com.hui.service.StoreProductRelationService;
import com.hui.util.ResponseResult;
import com.hui.util.module.GetUserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品收藏控制层
 */

@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/api/collect")
public class ProductCollectController {

    private final StoreProductRelationService storeProductRelationService;

    /**
     * 添加商品收藏
     *
     * @param queryParam
     * @param request
     * @return
     */
    @PostMapping("/add")
    public ResponseResult collectAdd(@RequestBody StoreProductRelationQueryParam queryParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        //判断商品id是否合法
        if (!NumberUtil.isNumber(queryParam.getId())) {
            throw new SystemException(AppHttpCodeEnum.NO_PARAMETE_OPERATE);
        }
        return storeProductRelationService.addRroductRelation(Long.valueOf(queryParam.getId()), uid, queryParam.getCategory());
    }

    /**
     * 取消商品收藏
     *
     * @param queryParam
     * @param request
     * @return
     */
    @ApiOperation(value = "取消收藏")
    @PostMapping("/del")
    public ResponseResult collectDel(@RequestBody StoreProductRelationQueryParam queryParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        //判断商品id是否合法
        if (!NumberUtil.isNumber(queryParam.getId())) {
            throw new SystemException(AppHttpCodeEnum.NO_PARAMETE_OPERATE);
        }
        return storeProductRelationService.delRroductRelation(Long.valueOf(queryParam.getId()), uid, queryParam.getCategory());
    }

    /**
     * 批量取消商品收藏 ，批量删除足迹
     *
     * @param
     * @param request
     * @return
     */
    @PostMapping("/dels/{productIds}")
    public ResponseResult collectDels(@PathVariable String productIds, @RequestBody StoreProductRelationQueryParam queryParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        //获取传来的ID
        String[] listid = productIds.split(",");
        if (listid.length > 0) {
            for (String id : listid) {
                storeProductRelationService.delRroductRelation(Long.valueOf(id), uid, queryParam.getCategory());
            }
        } else {
            throw new SystemException(AppHttpCodeEnum.NO_PARAMETE_OPERATE);
        }
        System.out.println(queryParam);
        return ResponseResult.okResult();
    }

    /**
     * 页码,默认为1
     * 页大小,默认为10
     * foot为足迹,collect为收藏
     */
    @GetMapping("/user")
    @ApiOperation(value = "获取收藏产品,或足迹", notes = "获取收藏产品,或足迹")
    public ResponseResult collectUser(CollectQueryParam collectQueryParam,
                                      HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);

        List<StoreProductRelationQueryVo> storeProductRelationQueryVos =
                storeProductRelationService.userCollectProduct(
                        collectQueryParam.getPage(),
                        collectQueryParam.getLimit(),
                        uid,
                        collectQueryParam.getType());

        return ResponseResult.okResult(storeProductRelationQueryVos);
    }
}
